/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package GUI.Components;

import Tools.ACommand;
import Tools.CMDThreadGUI;
import Tools.TModel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 *
 * @author Иван
 */
public class PanelCommandsButtons extends AComponentBuilder {

    private List<String> commands = new ArrayList<String>(0);
    private final TModel model;
    private JPanel panel = new JPanel();
    private final AObserverGUI observerGUI;
    private String commandName;

    public PanelCommandsButtons(TModel model, AObserverGUI observerGUI) {
        this.observerGUI = observerGUI;
        this.model = model;
    }
    private ActionListener action = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            commandName = e.getActionCommand();

            CMDThreadGUI threadGUI = new CMDThreadGUI(getUserCommand(commandName), observerGUI);
        }
    };

    @Override
    public JPanel buildComponent() throws Exception {
        JButton button;

        panel.setBackground(Color.red);

        List<Class<?>> declaredClasses = new ArrayList<Class<?>>(0);
        Field field = null;

        declaredClasses = Arrays.asList(model.getClass().getDeclaredClasses());

        for (Class<?> innerClass : declaredClasses) {
            if (innerClass.getSuperclass() == ACommand.class) {

                field = innerClass.getDeclaredField("commandName");
                field.setAccessible(true);
                commandName = (String) field.get(null);

                button = new JButton(commandName);

                button.addActionListener(action);

                panel.add(button);
            }
        }

        return panel;
    }

    public String getUserCommand(String commandName) {
        return observerGUI.buildCommand(commandName);
    }

    @Override
    public void updateComponent(Object object) {
    }
}
